package com.dcbeida.activity.features;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.CaptureActivity;
import com.dcbeida.activity.inventory.InventoryActivity;
import com.dcbeida.activity.mine.MineActivity;
import com.dcbeida.activity.msg.MsgActivity;
import com.dcbeida.activity.sales.StoresalesActivity;
import com.dcbeida.activity.store.StoreActivity;
import com.dcbeida.adapter.IndicatorViewPageAdapter;
import com.dcbeida.bean.MsgBean;
import com.dcbeida.utils.SharedPreferencesUtils;
import com.dcbeida.widget.IndicatorViewpager;
import com.squareup.otto.Subscribe;
import com.umeng.message.PushAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 首页
 */
public class FeaturesAcitivity extends BaseActivity implements View.OnClickListener, FeaturesContract.View {


    @Bind(R.id.lin_mdxs)
    LinearLayout lin_mdxs;

    @Bind(R.id.lin_kcpd)
    LinearLayout lin_kcpd;

    @Bind(R.id.lin_yprk)
    LinearLayout lin_yprk;

    @Bind(R.id.lin_wd)
    LinearLayout lin_wd;

    @Bind(R.id.lin_ypcx)
    LinearLayout lin_ypcx;


    @Bind(R.id.viewpager)
    IndicatorViewpager viewpager;

    @Bind(R.id.tv_shop_name)
    TextView tv_shop_name;

    @Bind(R.id.btnRight)
    ImageView btnRight;
    @Bind(R.id.btnRed)
    ImageView btnRed;

    private FeaturesPresenter mPresent;



    private PushAgent mPushAgent;
    private MyReceiver myReceiver;
    private MsgReceiver newMsg;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_features_acitivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lin_mdxs.setOnClickListener(this);
        lin_kcpd.setOnClickListener(this);
        lin_yprk.setOnClickListener(this);
        lin_wd.setOnClickListener(this);
        lin_ypcx.setOnClickListener(this);
        btnRight.setOnClickListener(this);


        IndicatorViewPageAdapter adapter = new IndicatorViewPageAdapter(this, getAdData());
        viewpager.updateIndicatorView(getAdData().size(), 0);
        viewpager.setAdapter(adapter);
        viewpager.startScorll();


        tv_shop_name.setText(MyApplication.user.getShopname());

        //BusProvider.getInstance().register(this);


        mPresent = new FeaturesPresenter(this, this);
        if(MyApplication.bindDevicetoken!=null)
            mPresent.bindDevicetoken(MyApplication.bindDevicetoken);



        myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(MyApplication.UPDATE_STATUS_ACTION);
        registerReceiver(myReceiver, filter);

        newMsg = new MsgReceiver();
        filter = new IntentFilter();
        filter.addAction(MyApplication.NEW_MSG);
        registerReceiver(newMsg, filter);


        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.onAppStart();
    }

    // 广告数据
    public static List<Integer> getAdData() {
        List<Integer> adList = new ArrayList<>();
        adList.add(R.drawable.img_top);

        return adList;
    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }

    @Override
    public void setPresenter(FeaturesContract.Present presenter) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lin_mdxs:
                showActivity(StoresalesActivity.class,null);

                break;
            case R.id.lin_kcpd:
                Bundle b = new Bundle();
                b.putInt("type", CaptureActivity.TYPE_PD);
                showActivity(InventoryActivity.class,b);
                break;
            case R.id.lin_yprk:
                showActivity(StoreActivity.class,null);

                break;
            case R.id.lin_wd:
                showActivity(MineActivity.class,null);
                break;
            case R.id.lin_ypcx:
                showActivity(CaptureActivity.class,null);

                break;
            case R.id.btnRight:
                showActivity(MsgActivity.class,null);
                break;
        }
    }

    /**
     * 实现再按一次退出提醒
     */
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 3000) {
                Toast.makeText(FeaturesAcitivity.this, R.string.snack_exit_once_more, Toast.LENGTH_LONG).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                android.os.Process.killProcess(android.os.Process.myPid());
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    SharedPreferencesUtils sp;
    @Override
    protected void onResume() {
        super.onResume();

        refreshMsg();

    }

    private void refreshMsg() {
        if(sp==null)
            sp = SharedPreferencesUtils.getInstance(this);
        if(sp.getIsHidden())//消息已读
            btnRed.setVisibility(View.GONE);
        else
            btnRed.setVisibility(View.VISIBLE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
        unregisterReceiver(newMsg);
//        BusProvider.getInstance().unregister(this);
    }

    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(MyApplication.bindDevicetoken!=null)
                mPresent.bindDevicetoken(MyApplication.bindDevicetoken);
        }
    }

    class MsgReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            refreshMsg();
        }
    }

//    //刷新消息标记
//    @Subscribe
//    public void refreshData(MsgBean event) {
//        refreshMsg();
//    }
}
